{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\".\\\\diyLogo.png\" alt=\"some_text\">\n",
    "<h1> 第三讲 程序设计基础</h1>\n",
    "<a id=backup></a>\n",
    "<H2>目录</H2>  \n",
    "\n",
    "[3.1 组合数据类型概述](#Section1)  \n",
    "[3.2 序列类型](#Section2)  \n",
    "[3.3 集合类型](#Section3)     \n",
    "[3.4 映射类型](#Section4)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id = Section1> </a>\n",
    "## 3.1 组合数据类型概述\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id = Section2> </a>\n",
    "## 3.2 序列类型\n",
    "### 字符串\n",
    "### 元组\n",
    "### 列表\n",
    "\n",
    "<p><a href=\"https://yanghailin.blog.csdn.net/article/details/81126087?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link\">\n",
    "this is example of python</a></p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.1字符串运算\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b 输出结果： HelloPython\n",
      "a * 2 输出结果： HelloHello\n",
      "a[1] 输出结果： e\n",
      "a[1:4] 输出结果： ell\n",
      "H 在变量 a 中\n",
      "M 不在变量 a 中\n",
      "\\n\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello\"\n",
    "b = \"Python\"\n",
    " \n",
    "print(\"a + b 输出结果：\", a + b)\n",
    "print(\"a * 2 输出结果：\", a * 2)\n",
    "print(\"a[1] 输出结果：\", a[1])\n",
    "print(\"a[1:4] 输出结果：\", a[1:4])\n",
    "# in \n",
    "if( \"H\" in a) :\n",
    "    print(\"H 在变量 a 中\")\n",
    "else :\n",
    "    print(\"H 不在变量 a 中\")\n",
    "#not in \n",
    "if( \"M\" not in a) :\n",
    "    print(\"M 不在变量 a 中\")\n",
    "else :\n",
    "    print(\"M 在变量 a 中\")\n",
    "# r R 直接打印字符串 \n",
    "print (r'\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.2元组\n",
    "Python 的元组与列表类似，不同之处在于元组的元素不能修改,可以合并。\n",
    "\n",
    "元组使用小括号 ( )，列表使用方括号 [ ]。\n",
    "\n",
    "元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Google', 'Runoob', 1997, 2000) \n",
      " tup2 \n",
      " ('a', 'b', 'c', 'd') \n",
      " <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tup1 = ('Google', 'Runoob', 1997, 2000)\n",
    "tup2 = (1, 2, 3, 4, 5 )\n",
    "tup3 = \"a\", \"b\", \"c\", \"d\"   #  不需要括号也可以\n",
    "type(tup3)\n",
    "print(tup1,\"\\n\",\"tup2\",\"\\n\",tup3,\"\\n\",type(tup3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2448321434544\n",
      "2448321266176\n"
     ]
    }
   ],
   "source": [
    "print(id(tup1))\n",
    "tup1=1,2,3\n",
    "print(id(tup1))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.3列表\n",
    "序列是 Python 中最基本的数据结构。\n",
    "\n",
    "序列中的每个值都有对应的位置值，称之为索引，第一个索引是 0，第二个索引是 1，依此类推。\n",
    "\n",
    "Python 有 6 个序列的内置类型，但最常见的是列表和元组。\n",
    "\n",
    "列表都可以进行的操作包括索引，切片，加，乘，检查成员。\n",
    "\n",
    "此外，Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。\n",
    "\n",
    "列表是最常用的 Python 数据类型，它可以作为一个方括号内的逗号分隔值出现。\n",
    "\n",
    "列表的数据项不需要具有相同的类型\n",
    "\n",
    "创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "white\n",
      "black\n"
     ]
    }
   ],
   "source": [
    "list = ['red', 'green', 'blue', 'yellow', 'white', 'black']\n",
    "print( list[0] )\n",
    "print( list[-2] )\n",
    "print( list[-1] )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list[0]:  Google\n",
      "list[0:4]:  ['Google', 'Runoob', 'Zhihu', 'Taobao', 'Wiki']\n"
     ]
    }
   ],
   "source": [
    "list = ['Google', 'Runoob', \"Zhihu\", \"Taobao\", \"Wiki\"]\n",
    " \n",
    "# 读取第二位\n",
    "print (\"list[0]: \", list[0])\n",
    "# 从第二位开始（包含）截取到倒数第二位（不包含）\n",
    "print (\"list[0:4]: \", list[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的第三个元素为 :  ['x', 'a', 2001, 'a', 't']\n",
      "更新后的列表 :  ['x', 'a', 2001, 'a', 't', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "list[2] = 2001\n",
    "print (\"更新后的第三个元素为 : \", list)\n",
    " \n",
    "list.append('Baidu')\n",
    "print (\"更新后的列表 : \", list)\n",
    "\n",
    "list[:]=''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除元素\n",
    "del list[2]\n",
    "print (\"删除第三个元素 : \", list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python列表函数&方法\n",
    "Python包含以下函数:\n",
    "<table><tr>\n",
    "<th style=\"width:5%\">序号</th><th style=\"width:95%\">函数</th></tr>\n",
    "\n",
    "<tr><td>1</td><td><a href=\"python3-att-list-len.html\">len(list)</a><br>列表元素个数</td></tr>\n",
    "<tr><td>2</td><td><a href=\"python3-att-list-max.html\">max(list)</a><br>返回列表元素最大值</td></tr>\n",
    "<tr><td>3</td><td><a href=\"python3-att-list-min.html\">min(list)</a><br>返回列表元素最小值</td></tr>\n",
    "<tr><td>4</td><td><a href=\"python3-att-list-list.html\">list(seq)</a><br>将元组转换为列表</td></tr>\n",
    "</table>\n",
    "\n",
    "Python包含以下方法:\n",
    "<table><tr>\n",
    "<th style=\"width:5%\">序号</th><th style=\"width:95%\">方法</th></tr>\n",
    "<tr><td>1</td><td><a href=\"python3-att-list-append.html\">list.append(obj)</a><br>在列表末尾添加新的对象</td></tr>\n",
    "<tr><td>2</td><td><a href=\"python3-att-list-count.html\">list.count(obj)</a><br>统计某个元素在列表中出现的次数</td></tr>\n",
    "<tr><td>3</td><td><a href=\"python3-att-list-extend.html\">list.extend(seq)</a><br>在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td></tr>\n",
    "<tr><td>4</td><td><a href=\"python3-att-list-index.html\">list.index(obj)</a><br>从列表中找出某个值第一个匹配项的索引位置</td></tr>\n",
    "<tr><td>5</td><td><a href=\"python3-att-list-insert.html\">list.insert(index, obj)</a><br>将对象插入列表</td></tr>\n",
    "<tr><td>6</td><td><a href=\"python3-att-list-pop.html\">list.pop([index=-1])</a><br>移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td></tr>\n",
    "<tr><td>7</td><td><a href=\"python3-att-list-remove.html\">list.remove(obj)</a><br>移除列表中某个值的第一个匹配项</td></tr>\n",
    "<tr><td>8</td><td><a href=\"python3-att-list-reverse.html\">list.reverse()</a><br>反向列表中元素</td></tr>\n",
    "<tr><td>9</td><td><a href=\"python3-att-list-sort.html\">\tlist.sort( key=None, reverse=False)</a><br>对原列表进行排序</td></tr>\n",
    "<tr><td>10</td><td><a href=\"python3-att-list-clear.html\">list.clear()</a><br>清空列表</td></tr>\n",
    "<tr><td>11</td><td><a href=\"python3-att-list-copy.html\">list.copy()</a><br>复制列表</td></tr>\n",
    "</table>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mitsubishi', 'Ford', 'BMW', 'VW']\n"
     ]
    }
   ],
   "source": [
    "# A function that returns the length of the value:\n",
    "def myFunc(e):\n",
    "  return len(e)\n",
    "cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']\n",
    "cars.sort(reverse=True, key=myFunc)\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mitsubishi', 'Ford', 'BMW']\n"
     ]
    }
   ],
   "source": [
    "cars.pop()\n",
    "print(cars)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=Section3></a>\n",
    "## 3.3 集合类型\n",
    "集合（set）是一个无序的不重复元素序列。\n",
    "\n",
    "可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3.1基本操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana','banana'}\n",
    "print(len(basket))                      # 这里演示的是去重功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{'orange', 'banana', 'pear', 'apple'}\n",
    "'orange' in basket                 # 快速判断元素是否在集合内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'crabgrass' in basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= {'r', 'c', 'b', 'd', 'a'}\n",
      "b= {'c', 'l', 'z', 'm', 'a'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'r'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面展示两个集合间的运算.\n",
    "a = set('abracadabrb')\n",
    "b = set('alacazam')\n",
    "print('a=',a)\n",
    "print('b=',b)\n",
    "# 集合a中包含而集合b中不包含的元素                                 \n",
    "a - b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合a或b中包含的所有元素（并集）\n",
    "a | b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'c'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合a和b中都包含了的元素(交集)\n",
    "a & b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不同时包含于a和b的元素（交集的补集）\n",
    "a ^ b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'r', 'd'}\n"
     ]
    }
   ],
   "source": [
    "#类似列表推导式，同样集合支持集合推导式(Set comprehension):\n",
    "a = {x  for x in 'abracadabra' if x not in 'abc'}\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3.2集合运算\n",
    "\n",
    "<table><tr>\n",
    "<th>方法</th>\n",
    "<th>描述</th>\n",
    "</tr>\n",
    "<tr><td><a href=\"ref-set-add.html\" target=\"_blank\" rel=\"noopener noreferrer\">add()</a></td><td>为集合添加元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-clear.html\" target=\"_blank\" rel=\"noopener noreferrer\">clear()</a></td><td>移除集合中的所有元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-copy.html\" target=\"_blank\" rel=\"noopener noreferrer\">copy()</a></td><td>拷贝一个集合</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-difference.html\" target=\"_blank\" rel=\"noopener noreferrer\">difference()</a></td><td>返回多个集合的差集</td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-difference_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">difference_update()</a></td><td>移除集合中的元素，该元素在指定的集合也存在。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-discard.html\" target=\"_blank\" rel=\"noopener noreferrer\">discard()</a></td><td>删除集合中指定的元素</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-intersection.html\" target=\"_blank\" rel=\"noopener noreferrer\">intersection()</a></td><td>返回集合的交集</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-intersection_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">intersection_update()</a></td><td>\n",
    "  返回集合的交集。</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-isdisjoint.html\" target=\"_blank\" rel=\"noopener noreferrer\">isdisjoint()</a></td><td>判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False。</td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-issubset.html\" target=\"_blank\" rel=\"noopener noreferrer\">issubset()</a></td><td>判断指定集合是否为该方法参数集合的子集。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-issuperset.html\" target=\"_blank\" rel=\"noopener noreferrer\">issuperset()</a></td><td>判断该方法的参数集合是否为指定集合的子集</td></tr>\n",
    "<tr><td><a href=\"ref-set-pop.html\" target=\"_blank\" rel=\"noopener noreferrer\">pop()</a></td><td>随机移除元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-remove.html\" target=\"_blank\" rel=\"noopener noreferrer\">remove()</a></td><td>移除指定元素</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-symmetric_difference.html\" target=\"_blank\" rel=\"noopener noreferrer\">symmetric_difference()</a></td><td>返回两个集合中不重复的元素集合。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-symmetric_difference_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">symmetric_difference_update()</a></td><td>\n",
    "  移除当前集合中在另外一个指定集合相同的元素，并将另外一个指定集合中不同的元素插入到当前集合中。 </td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-union.html\" target=\"_blank\" rel=\"noopener noreferrer\">union()</a></td><td>返回两个集合的并集</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-update.html\" target=\"_blank\" rel=\"noopener noreferrer\">update()</a></td><td>给集合添加元素</td></tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'basket' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_30752/2057266300.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 增加\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mbasket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Facebook\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbasket\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# 添加\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mbasket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Facebook\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'basket' is not defined"
     ]
    }
   ],
   "source": [
    "# 增加\n",
    "basket.add(\"Facebook\")\n",
    "print(basket)\n",
    "# 添加\n",
    "basket.update(\"Facebook\")\n",
    "print(basket)\n",
    "# 删除\n",
    "#basket.remove(\"apple\")\n",
    "print(basket)\n",
    "#basket.remove('none')  # throw error!!!\n",
    "basket.discard('none') # don't throw error!!!\n",
    "basket.pop()# throw one element random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'basket' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_30752/1118499116.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 计算集合长度\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mbasket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbasket\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbasket\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'basket' is not defined"
     ]
    }
   ],
   "source": [
    "# 计算集合长度\n",
    "basket.pop()\n",
    "print(basket)\n",
    "len(basket)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#清理集合\n",
    "basket.clear()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=Section4></a>\n",
    "## 3.4 映射类型\n",
    "字典是另一种可变容器模型，且可存储任意类型对象。\n",
    "字典的每个键值 key=>value 对用冒号 : 分割，每个对之间用逗号(,)分割，整个字典包括在花括号 {} 中 ,格式如下所示：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'graphviz'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_30752/2582578658.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mgraphviz\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mdot\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgraphviz\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDigraph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcomment\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'the round table'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"顺序结构\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnode_attr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'shape'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'box'\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mdot\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'1'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'字典'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mdot\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'2'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'name1：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mdot\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'3'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'key2：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'graphviz'"
     ]
    }
   ],
   "source": [
    "\n",
    "import graphviz\n",
    "dot=graphviz.Digraph(comment='the round table',name=\"顺序结构\",node_attr={'shape': 'box'})\n",
    "dot.node('1','字典')\n",
    "dot.node('2','name1：')\n",
    "dot.node('3','key2：')\n",
    "dot.node('4','key3：')\n",
    "dot.node('5','Value1')\n",
    "dot.node('6','Value2')\n",
    "dot.node('7','Value3')\n",
    "dot.edges(['12','13','14','25','36','47'])\n",
    "dot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本操作\n",
    "键必须是唯一的，但值则不必。\n",
    "\n",
    "值可以取任何数据类型，但键必须是不可变的，如字符串，数字。\n",
    "\n",
    "一个简单的字典实例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'zhangsan', 'Age': 18, 'url': 'www.xauat.edu.cn'}\n"
     ]
    }
   ],
   "source": [
    "dict = {'name': 'zhangsan', 'Age': 18, 'url': 'www.xauat.edu.cn'}\n",
    "print(dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 访问字典里的值  \n",
    "把相应的键放入到方括号中，如下实例:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict['name']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict['name']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改字典   \n",
    "向字典添加新内容的方法是增加新的键/值对，修改或删除已有键/值对如下实例:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'type' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_30752/1508947459.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"zhangsan\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'sex'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'男'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'type' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "dict['name']=\"zhangsan\"\n",
    "dict['sex']='男'\n",
    "print(dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数和方法\n",
    "<table><tr>\n",
    "<th>函数</th><th>返回值 ( 描述 )</th></tr>\n",
    "<tr><td><a> dict.keys() </a></td><td>返回所有键信息 </td></tr>\n",
    "<tr><td><a> dict.values() </a></td><td>返回所有值信息 </td></tr>\n",
    "<tr><td><a> dict.items() </a></td><td>返回所有键值对 </td></tr>\n",
    "<tr><td><a> dict.get('key',default) </a></td><td>键存在则返回相应值，否则返回默认值 </td></tr>\n",
    "<tr><td><a> dict.pop('key',default) </a></td><td> 键存在则返回相应值，否则删除删除键值对，返回默认值</td></tr>\n",
    "<tr><td><a> dict.popitem() </a></td><td>随机从字典中取出一个键值对 </td></tr>\n",
    "<tr><td><a> dict.clear() </a></td><td>删除所有的键值对 </td></tr>\n",
    "<tr><td><a> del dict['key'] </a></td><td>删除字典中某一个键值对 </td></tr>\n",
    "<tr><td><a> key in dict </a></td><td>如果键在字典中则返回True，否则返回False </td></tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('brand', 'Forddd'), ('model', 'Mustang'), ('year', 2018)])\n"
     ]
    }
   ],
   "source": [
    "\n",
    "car = {\n",
    "  'brand': 'Forddd',\n",
    "  'model': 'Mustang',\n",
    "  'year': 1964\n",
    "}\n",
    "x = car.items()\n",
    "car['year'] = 2018\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['brand', 'model', 'year'])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "x = True\n",
    "country_counter = {}\n",
    "\n",
    "def addone(country):\n",
    "    if country in country_counter:\n",
    "        country_counter[country] += 1\n",
    "    else:\n",
    "        country_counter[country] = 1\n",
    "\n",
    "addone('China')\n",
    "addone('Japan')\n",
    "addone('china')\n",
    "\n",
    "print(len(country_counter))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "x= True\n",
    "country_counter = {}\n",
    "\n",
    "def addone(country):\n",
    "    if country in country_counter:\n",
    "        country_counter[country] += 1\n",
    "    else:\n",
    "        country_counter[country] = 1\n",
    "\n",
    "addone('China')\n",
    "addone('Japan')\n",
    "addone('china')\n",
    "\n",
    "print(len(country_counter))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('python310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a888cd810c90be07c3c921372a6d7ff433f5a38d5779dfb2a871d99db75673fb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
